package com.huike.clues.service.impl;


import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.TbClueTrackRecord;
import com.huike.clues.domain.vo.ClueTrackRecordVo;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.clues.mapper.TbClueTrackRecordMapper;
import com.huike.clues.service.ITbClueTrackRecordService;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 线索跟进记录Service业务层处理
 *
 * @date 2022-04-22
 */
@Service
public class TbClueTrackRecordServiceImpl implements ITbClueTrackRecordService {

    @Autowired
    private TbClueTrackRecordMapper tbClueTrackRecordMapper;

    @Autowired
    private TbClueMapper tbClueMapper;

    @Override
    public TableDataInfo findList(Long clueId) {

        TableDataInfo tableDataInfo = new TableDataInfo(new ArrayList<>(), 0);

        //1.参数校验
        if (clueId == null) return tableDataInfo;

        //2.测试的代码信息
        List<TbClueTrackRecord> tbClueTrackRecords = tbClueTrackRecordMapper.findListByClueId(clueId);

        if (tbClueTrackRecords.size() == 0) return tableDataInfo;
        //3.封装返回值数据
        tableDataInfo.setRows(tbClueTrackRecords);
        tableDataInfo.setTotal(tbClueTrackRecords.size());
        tableDataInfo.setMsg("查询成功");
        tableDataInfo.setCode(200);

        //4.返回结果
        return tableDataInfo;
    }

    /**
     * 添加线索跟进记录的方法
     */
    @Override
    public AjaxResult addClueTrackRecord(ClueTrackRecordVo tbClueTrackRecord) {

        //1.参数的校验
        if (tbClueTrackRecord == null) return AjaxResult.error("参数异常");

        //2.调用方法修改clue主表的信息内容---微信号，年龄等信息
        TbClue tbClue = new TbClue();
        BeanUtils.copyProperties(tbClueTrackRecord,tbClue);
        tbClue.setId(tbClueTrackRecord.getClueId());
        tbClue.setStatus("2");
        tbClueMapper.updateTbClue(tbClue);

        //3.调用方法添加跟进记录表的信息
        tbClueTrackRecord.setType("0");
        tbClueTrackRecordMapper.insertClueTrackRecord(tbClueTrackRecord, SecurityUtils.getUsername(),new Date());

        //4.返回添加结果
        return AjaxResult.success("添加成功");
    }
}